package AutoJavaDoc;

$OPEN_COMMENT  = "/**";
$CLOSE_COMMENT = " */";
$COMMENT_START = " * ";
$linebreak = "\n";

###############################################################
# sub: checkClass
###############################################################
sub checkClass { #( $self, $inLine, $braceCount, $currentBrace, $inAuthor )
	my ( $self, $inLine, $braceCount, $currentBrace, $inAuthor ) = @_;
	# CHECK FOR CLASS...
	if (    $inLine =~ m/class/
		 && $inLine !~ m!//.*class!
		 && ( $braceCount - $currentBrace ) < 1 )
	{
		$main::isClass = 1;
		# Grab indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;
		
		$blankComment = $docIndent . $COMMENT_START;

		# strip open brace
		$inLine =~ s/{//;

		# Get the className
		# match [words or the "."] after "class "
		$inLine =~ m/class ([\w.]*)/;
		$className = $1;

		# Strip whitespace
		$inLine =~ s/\s//g;

		# Create the doc...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $docIndent . $COMMENT_START . $className . " ..." . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@usage <pre></pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@example <pre></pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		if($inAuthor) {
			$lineDoc .= $blankComment . '@author ' . $inAuthor . $linebreak;
		}
		# End the class comment.
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}

###############################################################
# sub: checkClassMethod
###############################################################
sub checkClassMethod { #( $self, $inLine, $braceCount, $currentBrace )
	my ( $self, $inLine, $braceCount, $currentBrace ) = @_;
	if (    $inLine =~ m/function/
		 && $inLine !~ m!//.*function!
		 && ( $braceCount - $currentBrace ) < 2
		 && $main::isClass == 1 )
	{
		# Calculate indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		$inLine =~ s/{//g;
		
		$blankComment = $docIndent . $COMMENT_START;
		
		# strip whitespace
		$inLine =~ s/\s//g;

		# extract Method Name
		$inLine =~ m/^.*function(.*)\(/;
		$methodName = $1;

		# extract parameters string
		$inLine =~ m/^.*\((.*)\)/;
		$parameters = $1;

		# create parameters documentation stub
		$parametersList = "";
		@params = split ",", $parameters;
		for $param (@params) {    # any LIST expression
			@farg = split ":", $param;
			$parametersList .=
			  $blankComment . '@param ' . $farg[0] . " - ..." . $linebreak;
		}

		# extract return type
		$matchReturnType = $inLine =~ m/\):(.*)/;
		if ($matchReturnType) {
			$returnType = $blankComment . '@return ' . $1;
		}
		# Method comments begin...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $blankComment . $methodName . " ..." . $linebreak;

		if ( $parametersList ne "" ) {
			$lineDoc .=  
				$blankComment . $linebreak
			  . $parametersList;
		}

		if ($matchReturnType) {
			$lineDoc .= 
			  	$blankComment . $linebreak
			  . $returnType . " ..." . $linebreak;
		}
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}
###############################################################
# sub: checkInterface
###############################################################
sub checkInterface { #( $self, $inLine, $braceCount, $currentBrace, $inAuthor )
	my ( $self, $inLine, $braceCount, $currentBrace, $inAuthor ) = @_;
	if (    $inLine =~ m/interface/
		 && $inLine !~ m!//.*interface!
		 && ( $braceCount - $currentBrace ) < 1 )
	{
		$main::isClass = 0;

		# Grab indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		$blankComment = $docIndent . $COMMENT_START;

		# strip open brace
		$inLine =~ s/{//;

		# Get the interfaceName
		# match [words or the "."] after "interface "
		$inLine =~ m/interface ([\w.]*)/;
		$interfaceName = $1;

		# Strip whitespace
		$inLine =~ s/\s//g;

		# Create the doc...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $docIndent . $COMMENT_START . $interfaceName . " ..." . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@usage <pre></pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@example <pre></pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@author ' . $inAuthor . $linebreak;
		# End the class comment.
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}
###############################################################
# sub: checkInterfaceMethod
###############################################################
sub checkInterfaceMethod { # ( $self, $inLine, $braceCount, $currentBrace )
	my ( $self, $inLine, $braceCount, $currentBrace ) = @_;
	if (    $inLine =~ m/function/
		 && $inLine !~ m!//.*function!
		 && ( $braceCount - $currentBrace ) < 2
		 && $main::isClass == 0 )
	{
		# Calculate indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		$inLine =~ s/{//g;
		
		$blankComment = $docIndent . $COMMENT_START;
		
		# strip whitespace
		$inLine =~ s/\s//g;

		# extract Method Name
		$inLine =~ m/^.*function(.*)\(/;
		$methodName = $1;

		# extract parameters string
		$inLine =~ m/^.*\((.*)\)/;
		$parameters = $1;

		# create parameters documentation stub
		$parametersList = "";
		@params = split ",", $parameters;
		for $param (@params) {    # any LIST expression
			@farg = split ":", $param;
			$parametersList .=
			  $blankComment . '@param ' . $farg[0] . " - ..." . $linebreak;
		}

		# extract return type
		$matchReturnType = $inLine =~ m/\):(.*)/;
		if ($matchReturnType) {
			$returnType = $blankComment . '@return ' . $1;
		}
		# Method comments begin...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $blankComment . $methodName . " ..." . $linebreak;

		if ( $parametersList ne "" ) {
			$lineDoc .=  
				$blankComment . $linebreak
			  . $parametersList;
		}

		if ($matchReturnType) {
			$lineDoc .= 
			  	$blankComment . $linebreak
			  . $returnType . " ..." . $linebreak;
		}
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}
1;
